package com.feib.soeasy.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;


/**
 * @title (#)DateUtil.java<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/11/14 create by Jimmy Liu<br>
 */
public class DateUtil 
{
    private static DateUtil instance = null;
    
    private DateUtil()
    {
        
    }

    public static DateUtil getInstance()
    {
        if (null == instance )
        {
            synchronized(DateUtil.class) {
                if (instance == null)
                    instance = new DateUtil();
            }
        }
        return instance;
    }


    public static Date getBeginOfDateTime(Date date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public static Date getEndOfDateTime(Date date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }
    
    public static String RocDateFormat(Date date)
    {
	if(null != date)
	{
	    SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
	    SimpleDateFormat mmFormat = new SimpleDateFormat("MM");
	    SimpleDateFormat ddFormat = new SimpleDateFormat("dd");
	    String rocYear = String.valueOf(new Integer(yearFormat.format(date)) - 1911);
	    String rocMonth = mmFormat.format(date);
	    String rocDay = ddFormat.format(date);
	
	    return rocYear + "年" + rocMonth + "月" + rocDay + "日";
	}
	return "";		
    }
    

    public static String getROCPayDueYearMon(Date date)
    {
    	if(null != date)
    	{
    	    SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
    	    SimpleDateFormat mmFormat = new SimpleDateFormat("MM");
    	    String rocYear = 
    	    	String.valueOf(new Integer(yearFormat.format(date)) - 1911);
    	    String rocMonth = mmFormat.format(date);
    	    
    	    return rocYear + "年" + rocMonth + "月";
    	}
    	return "";	
    }
    
    public static String getROCPayDue(Date date)
    {
    	if(null != date)
    	{
    	    SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
    	    SimpleDateFormat mmFormat = new SimpleDateFormat("MM");
    	    SimpleDateFormat ddFormat = new SimpleDateFormat("dd");
    	    String rocYear = 
    	    	String.valueOf(new Integer(yearFormat.format(date)) - 1911);
    	    String rocMonth = mmFormat.format(date);
    	    String rocDay = ddFormat.format(date);
    	    
    	    String weekDay = "";
    	    if(date.getDay() == 0)
    	    {
    	    	weekDay = "(日)";
    	    }else if(date.getDay() == 1)
    	    {
    	    	weekDay = "(一)";//20110608 黃郁珊 少了括弧
    	    }else if(date.getDay() == 2)
    	    {
    	    	weekDay = "(二)";
    	    }else if(date.getDay() == 3)
    	    {
    	    	weekDay = "(三)";
    	    }else if(date.getDay() == 4)
    	    {
    	    	weekDay = "(四)";
    	    }else if(date.getDay() == 5)
    	    {
    	    	weekDay = "(五)";
    	    }else if(date.getDay() == 6)
    	    {
    	    	weekDay = "(六)";
    	    }
    	    return rocYear + "年" + rocMonth + "月" + rocDay + "日" + weekDay;
    	}
    	return "";	
    }
    
    public static String numToCht(Long amount){
    	String[] units = new String[] {"拾", "佰", "仟", "萬", "拾", "佰", "仟", "億"};  

        String[] numeric = new String[] {"零", "壹", "貳", "參", "肆", "伍", "陸", "柒", "捌", "玖"};  
        String res = "";
        if(amount != null)
        {
        	String temp = String.valueOf(amount.intValue());
            for (int k = -1; temp.length() > 0; k++)  
            {  
                int j = Integer.parseInt(temp.substring(temp.length() - 1, temp.length()));  
                String rtemp = numeric[j];  
                if (j != 0 && k != -1 || k % 8 == 3 || k % 8 == 7)  
                {  
                    rtemp += units[k % 8];  
                }  

                res = rtemp + res;  
                temp = temp.substring(0, temp.length() - 1);  
            }  
 
            while (res.endsWith(numeric[0]))  
            {  
                res = res.substring(0, res.lastIndexOf(numeric[0]));  
            }  

            while (res.indexOf(numeric[0] + numeric[0]) != -1)  
            {  
                res = res.replaceAll(numeric[0] + numeric[0], numeric[0]);  
            }  

            for (int m = 1; m < units.length; m++)  
            {  
                res = res.replaceAll(numeric[0] + units[m], units[m]);  
            }  
              
            return res; 

        }  
        return "";
    }
   //黃郁珊 依中租需求修改
    public static String changeToComma(Long amount)
    {
    	if(amount != null)
    	{
    		DecimalFormat decimalFormat = new DecimalFormat("###,###,###");
    		String res = decimalFormat.format(amount);
    		return res;
    	}
    	return "";
    }
    /**
     * 目前時間是否在 d + minutes (不含)的時間範圍內.
     * 
     * @param d
     * @param minutes
     * @return
     */
    public static boolean isWithinMinutes(Date d, int minutes)
    {
            Date limit = DateUtils.addMinutes(d, minutes);
            return new Date().before(limit);
    }    
}
